package com.demo.example;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class GrayRoundRobinLoadBalancer {
//    private ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider;
//    private String serviceId;
//
//    @Override
//    public Mono<Response<ServiceInstance>> choose(Request request) {
//        ServiceInstanceListSupplier supplier = serviceInstanceListSupplierProvider.getIfAvailable(NoopServiceInstanceListSupplier::new);
//        return supplier.get(request).next().map(serviceInstances -> getInstanceResponse(serviceInstances, request));
//    }
//
//    Response<ServiceInstance> getInstanceResponse(List<ServiceInstance> instances, Request request) {
//        // 注册中心无可用实例 抛出异常
//        if (CollUtil.isEmpty(instances)) {
//            log.warn("No instance available {}", serviceId);
//            return new EmptyResponse();
//        }
//        DefaultRequestContext requestContext = (DefaultRequestContext) request.getContext();
//        RequestData clientRequest = (RequestData) requestContext.getClientRequest();
//        HttpHeaders headers = clientRequest.getHeaders();
//        String reqVersion = headers.getFirst(CommonConstants.VERSION);
//        if (StrUtil.isBlank(reqVersion)) {
//            return super.choose(request).block();
//        }
//        // 遍历可以实例元数据，若匹配则返回此实例
//        for (ServiceInstance instance : instances) {
//            NacosServiceInstance nacosInstance = (NacosServiceInstance) instance;
//            Map<String, String> metadata = nacosInstance.getMetadata();
//            String targetVersion = MapUtil.getStr(metadata, CommonConstants.VERSION);
//            if (reqVersion.equalsIgnoreCase(targetVersion)) {
//                log.debug("gray requst match success :{} {}", reqVersion, nacosInstance);
//                return new DefaultResponse(nacosInstance);
//            }
//        }  // 降级策略，使用轮询策略
//         return super.choose(request).block(); }
        }
